#include "GatewayLoginSucceedCmd.h"

GatewayLoginSucceedCmd::GatewayLoginSucceedCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
}

Package GatewayLoginSucceedCmd::toPackage()
{
    Package package;
    return package;
}

void GatewayLoginSucceedCmd::unPackage(Package package)
{
    m_cmdId = package.id();

    QByteArray body;
    quint16 strLength,index = 0;
    quint16 strListSize;

   // body = decrypt(package.body(),KeyType::AES);

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QString fatherAccount = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QString fatherPassword = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QString fatherName = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QString fatherRole = body.mid(index,strLength);
    index += strLength;

    strListSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QStringList fatherPermissionList;
    for(int i=0; i<strListSize; i++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString permission = body.mid(index,strLength);
        index += strLength;
        fatherPermissionList.append(permission);
    }
    UserStruct fatherUser;
    fatherUser.setAccount(fatherAccount);
    fatherUser.setPassword(fatherPassword);
    fatherUser.setName(fatherName);
    fatherUser.setRole(fatherRole);
    fatherUser.setPermissionList(fatherPermissionList);
    fatherUser.setIsPrimary(true);
    m_userList.append(fatherUser);

    quint16 childUserSize = body.mid(index,2).toHex().toInt(0,16);
    for(int i=0; i<childUserSize; i++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString childAccount = body.mid(index,strLength);
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString childPassword = body.mid(index,strLength);
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString childName = body.mid(index,strLength);
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString childRole = body.mid(index,strLength);
        index += strLength;

        strListSize = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QStringList childPermissionList;
        for(int i=0; i<strListSize; i++)
        {
            strLength = body.mid(index,2).toHex().toInt(0,16);
            index += 2;
            QString permission = body.mid(index,strLength);
            index += strLength;
            childPermissionList.append(permission);
        }
        UserStruct childUser;
        childUser.setAccount(childAccount);
        childUser.setPassword(childPassword);
        childUser.setName(childName);
        childUser.setRole(childRole);
        childUser.setPermissionList(childPermissionList);
        childUser.setIsPrimary(false);
        m_userList.append(childUser);
    }

}
QList<UserStruct> GatewayLoginSucceedCmd::userList() const
{
    return m_userList;
}

void GatewayLoginSucceedCmd::setUserList(const QList<UserStruct> &userList)
{
    m_userList = userList;
}

